import QtQuick 1.1
import com.nokia.meego 1.0
import "Logic.js" as Logic

Page {
    id: pageDialogNetworkInterfaces
    objectName: "pageDialogNetworkInterfaces"
    orientationLock: appWindow.lockOrientation()

    property int curr_iface: 0

    tools: ToolBarLayout {
        id: tool_only_back
        ToolIcon {
            id: toolIconBack
            iconId: "toolbar-back"
            onClicked: { pageStack.pop(); }
        }
        ToolIcon {
            id: toolIconRemove
            iconId: "toolbar-delete"
            onClicked: {
                dlgDeleteIface.open()
            }
        }
        ToolIcon {
            id: toolIconAdd
            iconId: "toolbar-add"
            onClicked: { sheetPageAddIface.open() }
        }
    }

    //======================================
    function setTextIfconfig( textIfcfg ) {
        tfIfconfig.text = textIfcfg
    }

    //**************************************
    QNMTitle {
        id: titlePage
        text: qsTr("Network interfaces")
        image: "./pictures/options.png"
    }

    BackgroundPage { }
    //**************************************

    Item {
        id: gr_Ifaces
        //anchors.centerIn: parent
        anchors.top: titlePage.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.leftMargin: 16
        anchors.rightMargin: 16
        anchors.bottomMargin: 5
        anchors.topMargin: 5
        width: parent.width //!
        height: parent.height - mytheme.pageTitleAreaHeight

        Image {
             id: imgUpdateIfconfig
             z: 1
             anchors.right: flicktextArea.right
             anchors.rightMargin: 10
             smooth: true

             states: [
                 State {
                     name: "rotated"
                     PropertyChanges { target: imgUpdateIfconfig; rotation: 180; }
                 },
                 State {
                     name: "rotated_back"
                     PropertyChanges { target: imgUpdateIfconfig; rotation: 0; }
                 }
             ]
             transitions: Transition { RotationAnimation {duration: 700; direction: RotationAnimation.Counterclockwise} }

             MouseArea {
                 anchors.fill: parent
                 onClicked: {
                     if ( (imgUpdateIfconfig.state == "rotated_back") || (imgUpdateIfconfig.state == "") ) {
                         imgUpdateIfconfig.state = "rotated"
                     } else {
                         imgUpdateIfconfig.state = "rotated_back"
                     }
                     imgUpdateIfconfig.enabled = false
                     cppOptIfces.initIfconfig()
                 }
             }
         }

        Flickable {
            id: flicktextArea
            anchors.top: parent.top
            anchors.left: parent.feft
            width: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.width/2 : parent.width
            height: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.height : parent.height/2
            contentWidth: tfIfconfig.width
            contentHeight: tfIfconfig.height
            clip: true

            TextEdit {
               id: tfIfconfig

               width: flicktextArea.width

               readOnly: true
               textFormat: TextEdit.RichText
               text: ""
               color: mytheme.fontMainColor
               onTextChanged: {
                   if( theme.inverted === false )
                       imgUpdateIfconfig.source = "image://theme/icon-m-toolbar-refresh"
                   else
                       imgUpdateIfconfig.source = "image://theme/icon-m-toolbar-refresh-white"
                   imgUpdateIfconfig.enabled = true
               }
           }
        }

        ScrollDecorator { flickableItem: flicktextArea }

        //-----------------------------
        Rectangle {
            id: r_splitter
            width: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 10 : parent.width
            height: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.height : 10

            anchors.top: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.top : flicktextArea.bottom
            anchors.left: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? flicktextArea.right : parent.left

            radius: 5
            gradient: Gradient {
                GradientStop { position: 0; color: "lightgray" }
                GradientStop { position: 0.5; color: "gray" }
                GradientStop { position: 1; color: "lightgray" }
            }
        }

        //-----------------------------

        ListView {
            id: listview_ifaces

            onVisibleChanged: {
                if( visible ) {
                    listview_ifaces.currentIndex = 0
                }
            }

            width: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.width/2 : parent.width
            height: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.height : parent.height/2

            //anchors.top: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.top : flicktextArea.bottom
            //anchors.left: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? flicktextArea.right : parent.left
            anchors.top: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.top : r_splitter.bottom
            anchors.left: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? r_splitter.right : parent.left


            //anchors.leftMargin: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 10 : 0
            anchors.rightMargin: 10

            clip: true

            //компонент пункта меню
            Component {
                id: ifaceItemDelegate
                Item {
                    id: ifaceItemWrapper
                    signal clicked
                    property alias pressed: mouseAreaItemIface.pressed
                    width: listview_ifaces.width
                    height: 90

                    Row {
                        id: rowItemIface
                        anchors.verticalCenter: parent.verticalCenter
                        spacing: 15

                        Image {
                            id: imgIface
                            anchors.verticalCenter: parent.verticalCenter
                            source: ifacePicture
                        }

                        Text {
                            id: textInfoIface
                            anchors.verticalCenter: parent.verticalCenter
                            text: ifaceName;
                            font.pixelSize: 36;
                            color: mytheme.fontMainColor
                        }
                    } //Row

                    states: State {
                        name: "Current"
                        when: ifaceItemWrapper.ListView.isCurrentItem
                        PropertyChanges { target: ifaceItemWrapper; x: 20 }
                        PropertyChanges { target: textInfoIface; color: mytheme.fontAltColor }
                    }

                    transitions: Transition {
                        NumberAnimation { properties: "x"; duration: 200 }
                    }

                    MouseArea {
                        id: mouseAreaItemIface;
                        anchors.fill: parent
                        onClicked: {
                            parent.ListView.view.currentIndex = index
                            ifaceItemWrapper.clicked();
                            curr_iface = index
                        }
                    }

                } //Item
            } //Component

            //компонент подсветки пункта меню
            Component {
                id: ifaceItemHighlight
                Rectangle {
                    height: 90
                    width: listview_ifaces.width
                    gradient: Gradient {
                        GradientStop { position: 0; color: mytheme.grColor2 }
                        GradientStop { position: 1; color: mytheme.grColor3 }
                    }
                    border.color: mytheme.pageTitleAreaColor
                    radius: 10
                    y: listview_ifaces.currentItem.y;
                    Behavior on y { SmoothedAnimation { velocity: 500 } }
                }
            }

            model: listIfacesMonitor
            delegate: ifaceItemDelegate
            highlight: ifaceItemHighlight
            highlightFollowsCurrentItem: true
            focus: true
        } //ListView

    } //Item

    /*Connections {
        target: cppOptIfces
        onListModelIfacesChanged: {
            console.log( "QML: onListModelIfacesChanged" )
            listview_ifaces.model = cppOptIfces.cppModelListIfaces
        }
    }*/

    //====================================
    Sheet {
        id: sheetPageAddIface

        acceptButtonText: qsTr("Save")
        rejectButtonText: qsTr("Cancel")

        content: Flickable {
            anchors.fill: parent
            anchors.leftMargin: 20
            anchors.rightMargin: 20
            anchors.topMargin: 20
            contentWidth: col2.width
            contentHeight: col2.height
            flickableDirection: Flickable.VerticalFlick

            Column {
                id: col2
                anchors.top: parent.top
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: 25

                Row {
                    spacing: 20
                    Image {
                        anchors.verticalCenter: parent.verticalCenter
                        source: "pictures/netdevice.png"
                    }
                    Label {
                        font.pixelSize: 28
                        anchors.verticalCenter: parent.verticalCenter
                        text: qsTr("Adding network interface")
                    }
                }
                TextField {
                    width: (screen.orientationString == "Portrait") || (screen.orientationString == "PortraitInverted") ? 440 : 810
                    id: tfIfaceName
                    placeholderText: qsTr("Name of network interface")
                }

            } //Column
        } //Flickable

        onAccepted: {
            cppOptIfces.addIface( tfIfaceName.text )
        }

    }
    //====================================
    function checkTurnOffIfaces() {
        var cnt = listview_ifaces.model.count
        for( var j=0; j < cnt; j +=1 ) {
            var txt = pyOptIfacesList.get(j).text
            console.log( txt )
        }
    }

    //====================================

    /*
    DialogWarning {
        id: dlgTurnOffRecord
        text_dialog: qsTr("Before you disable/delete the network interface, turn off the record on this network interface")
    }

    function showDlgTurnOffRecord() {
        dlgTurnOffRecord.open()
    }
    */
    //----------------------

    /*
    DialogWarning {
        id: dlgOneIface
        text_dialog: qsTr("Should be left on at least one network interface")
    }

    function showDlgOneIface() {
        dlgOneIface.open()
    }
    */
    //----------------------

    /*
    DialogWarning {
        id: dlgCantDeleteIface
        text_dialog: qsTr("Network interface cannot be deleted when the database has recorded net session at the interface")
    }

    function showDlgCantDeleteItem() {
        dlgCantDeleteIface.open()
    }
    */
    //----------------------

    DialogWarning {
        id: dlgCantDeleteAllIface
        text_dialog: qsTr("You can not delete all the network interfaces")
    }

    function showDlgCantDeleteAllIface() {
        dlgCantDeleteAllIface.open()
    }

    //----------------------

    DialogWarning {
        id: dlgIfaceExists
        text_dialog: qsTr("Specified network interface already exists")
    }

    function showDlgIfaceExists() {
        dlgIfaceExists.open()
    }

    //----------------------
    /*
    DialogError {
        id: dlgErrorDB
        text_dialog: qsTr("Error to the database")
    }

    function showDlgErrorDB() {
        dlgErrorDB.open()
    }
    */
    //----------------------

    QueryDialog {
        id: dlgDeleteIface
        message: qsTr("Delete the selected network interface ?")
        acceptButtonText: qsTr("Yes")
        rejectButtonText: qsTr("No")
        icon: "image://theme/icon-m-toolbar-delete-white"
        onAccepted: cppOptIfces.removeIface( curr_iface )
    }


    //====================================

}
